package com.xja.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.xja.entity.Bookinfo;
import com.xja.entity.Orderinfo;
import com.xja.entity.PageResult;
import com.xja.entity.Userinfo;
import com.xja.feignclient.OrderService;
import com.xja.service.UserinfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 周广瑞
 * @date 2022/4/26 11:31
 */
@RestController
@RequestMapping("user")

public class UserController {
    private Logger logger= LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private OrderService orderService;


    @GetMapping("find")
    public PageResult<List<Bookinfo>>findall(){
            logger.info("调用了订单业务查询所有的信息");
        List<Userinfo> userinfoList = userinfoService.slectUser();
        System.out.println("调用了订单业务查询所有的信息"+userinfoList);
        return PageResult.success("成功",userinfoList);
    }

    //查询用户以及用户的订单列表
    @GetMapping("finduid")
    public PageResult<Map<String,Object>>findUid(@RequestParam("uid")Integer uid){
        logger.info("调用了查询用户以及用户订单的方法");
        Userinfo user = userinfoService.selectByPrimaryKey(uid);
        if (user ==null)
            return PageResult.nullresult("用户不存在",null);
        PageResult<List<Orderinfo>> pageResult = orderService.findUid(user.getUserid());
        List<Orderinfo> orderinfoList = pageResult.getObj();
        Map<String,Object>userMap = new HashMap<>();
        userMap.put("user",user);
        userMap.put("orderinfoList",orderinfoList);

        return PageResult.success("成功",userMap);
    }


    //按照用户id 查询用户信息
    @RequestMapping("finduserbyuid")
    public PageResult<Userinfo>findUserByUid(@RequestParam("uid")Integer uid){
        logger.info("调用了订单业务查询所有的信息");
                Userinfo user = userinfoService.selectByPrimaryKey(uid);
        return PageResult.success("成功",user);

    }





}
